package com.lzkj.baotouhousingfund.http;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Chengs on 16/7/22.
 */
public class UploadUtils {

    public static MultipartBody filesToMultipartBody(String key, List<File> files) {
        MultipartBody.Builder builder = new MultipartBody.Builder();
        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
                    file);
            builder.addFormDataPart(key, file.getName(), requestBody);
        }

        builder.setType(MultipartBody.FORM);
        return builder.build();
    }

    public static List<MultipartBody.Part> filesToMultipartBodyParts(String key, List<File> files) {
        List<MultipartBody.Part> parts = new ArrayList<>(files.size());
        for (File file : files) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
                    file);
            MultipartBody.Part part = MultipartBody.Part.createFormData(key, file.getName(),
                    requestBody);
            parts.add(part);
        }
        return parts;
    }

    public static RequestBody stringToRequestBody(String text) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), text);
        return requestBody;
    }

    public static RequestBody jsonToRequestBody(String text) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;" +
                "charset=UTF-8"), text);
        return requestBody;
    }

    public static RequestBody fileToRequestBody(String path) {
        File file = new File(path);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        return requestBody;
    }

    public static MultipartBody.Part fileToMultipartBodyPart(String key, String path) {
        File file = new File(path);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part part = MultipartBody.Part.createFormData(key, file.getName(),
                requestBody);
        return part;
    }

    public static MultipartBody.Part fileToMultipartBodyPart(String key, File file) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part part = MultipartBody.Part.createFormData(key, file.getName(),
                requestBody);
        return part;
    }


    public static Map<String, RequestBody> filesToRequestBodyMap(List<File> files) {

        Map<String, RequestBody> params = new HashMap<>();
        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
                    file);
            params.put("imagesfile[]", requestBody);
        }
//        picture"; filename="
        return params;
    }

    public static Map<String, MultipartBody.Part> filesToPartMap(List<File> files) {

        Map<String, MultipartBody.Part> params = new HashMap<>();
        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
                    file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("imagesfile[]", file
                    .getName(), requestBody);
            params.put("imagesfile[]", part);
        }
//        picture"; filename="
        return params;
    }

    public static List<RequestBody> filesToRequestBodys(List<File> files) {

        List<RequestBody> requestBodies = new ArrayList<>();
        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
                    file);
            requestBodies.add(requestBody);
        }
//        picture"; filename="
        return requestBodies;
    }
}
